﻿package com.dykj.apps.web.sysmanager.dto;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author zhaolp
 * @time 2009-3-24
 */
public class Tree {
	private String functionid;
	private String title;
	private String href;
	private String checked;
	private Tree parent;
	private List<Tree> childs = new ArrayList<Tree>();

	public Tree() {
	}


	public Tree(String functionid, String title, String href,String checked, Tree parent) {
		this.functionid = functionid;
		this.title = title;
		if (href == null)
			this.href = "";
		else
			this.href = href;
		this.checked = checked;
		this.parent = parent;
		if (this.parent != null) {
			this.parent.addChild(this);
		}
	}
	
	public boolean addChild(Tree Tree) {
		return this.childs.add(Tree);
	}

	public Iterator<Tree> getChildIterator() {
		return childs.iterator();
	}

	public List<Tree> getChilds() {
		return this.childs;
	}


	public String getFunctionid() {
		return functionid;
	}


	public void setFunctionid(String functionid) {
		this.functionid = functionid;
	}

	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public Tree getParent() {
		return parent;
	}

	public void setParent(Tree parent) {
		this.parent = parent;
	}

	public int level() {
		int result = 0;
		while (this.parent != null) {
			result++;
		}
		return result;
	}

	public boolean hasChilds() {
		return !this.childs.isEmpty();
	}

	public Tree getFirstLeaf() {
		if (this.hasChilds()) {
			return this.childs.get(0).getFirstLeaf();
		}
		return this;
	}

	public Tree getTopParent() {
		if (this.parent == null) {
			return this;
		} else {
			return this.parent.getTopParent();
		}
	}

	public List<Tree> getTreePath() {
		List<Tree> list = new ArrayList<Tree>();
		Tree tmp = this.getParent();
		while (tmp != null) {
			list.add(tmp);
			tmp = tmp.getParent();
		}
		return list;
	}

	public String getStringPath() {
		StringBuffer sb = new StringBuffer();
		List<Tree> tmp = this.getTreePath();
		for (int i = tmp.size() - 1; i >= 0; i--) {
			Tree Tree = tmp.get(i);
			sb.append(Tree.getTitle() + ".");
		}
		sb.append(this.getTitle());
		return sb.toString();
	}


	public String getHref() {
		return href;
	}


	public void setHref(String href) {
		this.href = href;
	}


	public String getChecked() {
		return checked;
	}

	public void setChecked(String checked) {
		this.checked = checked;
	}


}